// pages/promotionTool/promotionTool.js
const app = getApp()
Page({
  data: {
    hasShop:false,
    logined: false
  },
  onLoad: function (options) {
    if (app.globalData.sessionId !== undefined) {
      if(app.globalData.userInfo.hasShop){
        this.setData({
          hasShop: true
        })
      }else{
        this.setData({
          hasShop: false
        })
      }
      this.setData({
        logined: true
      })
    }else{
      this.setData({
        logined: false
      })
    }
  },
  onShow(){
    this.loginModal = this.selectComponent("#loginModal");
  },
  toSolitaire(){
    if(app.globalData.sessionId && app.globalData.userInfo.hasShop){
      wx.navigateTo({
        url: '/pages/initiateJl/initiateJl',
        success: function (res) {
          console.log('toInitiateJl')
        },
        fail: function (res) {
          console.log(res)
        }
      })
    }else if(app.globalData.sessionId){
      this.createShop()
    }else{
      this.loginTip()
    }
    
  },
  loginTip() {
    // const that = this
    // app.globalData.sessionId = undefined
    // wx.showModal({
    //   title: '提示',
    //   content: '你需要登录，才能使用推广工具',
    //   success(res) {
    //     if (res.confirm) {
    //       console.log('用户点击确定')
    //       that.runLogin()
    //     } else if (res.cancel) {
    //       console.log('用户点击取消')
    //     }
    //   }
    // })
    this.loginModal.showPopup();
  },
  createShop() {
    const that = this
    app.globalData.sessionId = undefined
    wx.showModal({
      title: '提示',
      content: '你需要升级会员才能使用推广工具，升级会员享受更多权益',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.switchTab({
            url: '/pages/myStore/myStore',
            success: function (res) {
              console.log('toInitiateJl')
            },
            fail: function (res) {
              console.log(res)
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  toFreeList(){
    if(app.globalData.sessionId && app.globalData.userInfo.hasShop){
      wx.navigateTo({
        url: '/pages/freeItem/freeItem?from=free',
        success: function (res) {
          console.log('toInitiateJl')
        },
        fail: function (res) {
          console.log(res)
        }
      })
    }else if(app.globalData.sessionId){
      this.createShop()
    }else{
      this.loginTip()
    }
    
  },
  runLogin() {
    wx.showLoading({
      title: '登录中',
    })
    let res = app.login(app.globalData.thisshareTicket)
    this.waitLogin()
  },
  waitLogin() {
    const that = this
    let promise = new Promise((resolve, reject) => {
      let timer = setInterval(() => {
        // console.log(app.globalData.sessionId,app.globalData.userId)
        if (app.globalData.sessionId && app.globalData.userId) {
          clearInterval(timer);
          resolve(app.globalData);
          that.setData({
            logined: true,
            userInfo: app.globalData.userInfo
          })
          console.log(that.data.logined)
          wx.hideLoading()
          that.getTabBar()
        } else {
          //todo 信息构建中
        }
      }, 250);
    });
    
  },
  toStore(){
    wx.switchTab({
      url: '/pages/myStore/myStore',
      success: function (res) {
        console.log('toInitiateJl')
      },
      fail: function (res) {
        console.log(res)
      }
    })
  }
})